import {
  myRequesy
} from '../../request/api.js'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    //活动选项卡数组
    actiBer: [{
      id: 1,
      name: "预告",
      status: false,
      icon: '/static/img/alarm.png',

    }, {
      id: 0,
      name: "进行中",
      status: true,
      icon: '/static/img/proceed.png',
    }, ],
    current: 0, //轮播索引
    // 年份数组
    yearList: [{
        year: 2021
      },
      {
        year: 2022
      }
    ],

    UnderwayList: [], //活动进行中数据列表
    UnderwayNextPage: null, //判断活动进行中是否有下一页数据
    UnderwayNum: 1, //活动进行中页码值
    activeIndex: 0, //判断预告当前年份
    forecastList: [], //活动预告数据列表
    forecastNextPage: null, //判断活动预告是否有下一页数据
    forecastNum: 1, //活动预告页码值
    adsList: [], //广告列表数据
    shut: true, //控制新人专享
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      state: options.state
    })

    this.getUnderwayList()
    this.getabsList()
  },
  // 顶部选项卡回调
  btntab(e) {
    let token = wx.getStorageSync('userInfo').token
    if (!token) {
      this.getUserLogin()
    } else {
      let {
        index
      } = e.currentTarget.dataset
      let {
        actiBer
      } = this.data
      actiBer.forEach((item, i) => i === index ? item.status = true : item.status = false)
      this.setData({
        actiBer,
        UnderwayNum: 1,
        forecastNum: 1,
        activeIndex: 0
      })
      if (index === 0) {
        this.getPreviewList()
      } else {
        this.getUnderwayList()
      }
    }

  },

  //获取轮播索引
  SwiperChenge(e) {
    let {
      current
    } = e.detail
    this.setData({
      current
    })
  },

  //跳转到活动详情
  navDuct(e) {
    wx.navigateTo({
      url: '/pages/hddetails/hddetails?id=' + e.currentTarget.dataset.id,
    })
  },




  //获取活动进行中列表
  getUnderwayList() {
    let that = this
    myRequesy({
      url: 'activity/starts',
      data: {
        page: that.data.UnderwayNum
      }
    }).then(res => {
      if (res.data.code === 0) {
        that.setData({
          UnderwayList: res.data.data,
          UnderwayNextPage: res.data.next_url
        })
      }
    })
  },

  // 获取活动进行种下一页数据
  getUnderwayNextList() {
    let that = this
    if (that.data.UnderwayNextPage !== null) {
      that.data.UnderwayNum += 1
      myRequesy({
        url: 'activity/starts',
        data: {
          page: that.data.UnderwayNum
        }
      }).then(res => {
        if (res.data.code === 0) {
          that.setData({
            UnderwayList: [...that.data.UnderwayList, ...res.data.data],
            UnderwayNextPage: res.data.next_url
          })
        }
      })
    }
  },

  // 获取活动预告列表数据
  getPreviewList() {
    let that = this
    let {
      yearList,
      activeIndex,
      forecastNum
    } = that.data
    myRequesy({
      url: 'activity/un-starts',
      data: {
        year: yearList[activeIndex].year,
        page: forecastNum
      }
    }).then(res => {
      if (res.data.code === 0) {
        that.setData({
          forecastList: res.data.data,
          forecastNextPage: res.data.next_url
        })
      }
    })
  },
  // 获取活动预告下一页列表数据
  getPreviewNextList() {
    let that = this
    let {
      yearList,
      activeIndex,
      forecastNum,
      forecastNextPage
    } = that.data
    if (forecastNextPage !== null) {
      that.data.forecastNum += 1
      myRequesy({
        url: 'activity/un-starts',
        data: {
          year: yearList[activeIndex].year,
          page: forecastNum
        }
      }).then(res => {
        if (res.data.code === 0) {
          that.setData({
            forecastList: [...that.data.forecastList, ...res.data.data],
            forecastNextPage: res.data.next_url
          })
        }
      })
    }
  },

  //活动预告年份上一年
  headleSubtract() {
    let {
      activeIndex
    } = this.data
    if (activeIndex >= 1) {
      activeIndex -= 1
      this.setData({
        activeIndex,
        forecastNum: 1
      })
      this.getPreviewList()
    }
  },

  //活动预告年份下一年
  headleAdd() {
    let {
      activeIndex
    } = this.data
    if (activeIndex === 0) {
      activeIndex += 1
      this.setData({
        activeIndex,
        forecastNum: 1
      })
      this.getPreviewList()
    }
  },


  // 活动预告订阅通知
  headleMake(e) {
    let that = this
    if (!e.currentTarget.dataset.status) {
      wx.requestSubscribeMessage({
        tmplIds: ['8pUorSTDgk0nqAU7v6GxRM3LP2RY_EmDgcrimSx1X0Q', 'iXO6nRRt8g0Op1YQg9gs7WfytX9-vj8O3ml3sqjx7z0', 'Ke1p0Jn84Q_5XVYM6TT5aWb1Z4-oy7bJ2odnnH88kOE'],
        success() {
          myRequesy({
            url: 'subscribe/add',
            method: 'POST',
            data: {
              tag_id: e.currentTarget.dataset.id
            }
          }).then(res => {
            if (res.data.code === 0) {
              wx.showToast({
                title: '订阅成功',
              })
              that.getPreviewList()
            }
          })
          return
        }
      })

    }

  },

  // 跳转到新人用户页
  headleTask() {
    let token = wx.getStorageSync('userInfo').token
    if (token) {
      wx.navigateTo({
        url: '/pages/mission/mission',
      })
      return
    }
    this.getUserLogin()

  },

  //获取轮播图广告列表
  getabsList() {
    let that = this
    myRequesy({
      url: 'ads'
    }).then(res => {
      if (res.data.code === 0) {
        that.setData({
          adsList: res.data.data
        })
      }
    })
  },

  //获取用户信息
  getUserLogin() {
    let that = this
    wx.getUserProfile({
      desc: '获取您的用户名和头像',
      success(res1) {
        wx.login({
          success(res2) {
            console.log(res2);
            let userData = {
              code: res2.code,
              nickName: res1.userInfo.nickName,
              avatarUrl: res1.userInfo.avatarUrl
            }
            myRequesy({
              url: 'Login',
              data: userData,
              method: 'POST'
            }).then(result => {
              wx.setStorage({
                data: result.data.data,
                key: 'userInfo',
                success() {
                  that.onLoad()
                }
              })

            })
          }
        })
      }
    })
  },

  //转发海报
  headlePoster(e) {
    console.log(e);
    let {
      id,
      img,
      title
    } = e.currentTarget.dataset
    wx.navigateTo({
      url: '/pages/poster/poster?id=' + id + '&name=' + title + '&backImg=' + img,

    })
  },

  // 第一张轮播图跳转到大赛页
  headleNavMega() {
    wx.navigateTo({
      url: '/pages/megagame/megagame',
    })
  },
  // 第二张轮播图跳转到新人任务页
  headleNavMiss() {
    wx.navigateTo({
      url: '/pages/mission/mission',
    })
  },

  //跳转到对应的小程序或web-view
  headleNavProgram(e) {
    let {
      appid,
      state,
      path,
      url
    } = e.currentTarget.dataset.item
    if (state == 0) {
      wx.navigateToMiniProgram({
        appId: appid,
        path,
        success(res) {},
      })
      return
    }
    let data = encodeURIComponent(url)
    wx.navigateTo({
      url: '/pages/webView/webView?url=' + data,
    })

  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    if (this.data.actiBer[0].status) {
      this.getPreviewList()
    } else {
      this.getUnderwayList()
    }
    if (this.data.state) {
      setTimeout(() => {
        myRequesy({
          url: 'user/to-view',
          method: 'POST',
        }).then(res => {
          console.log(res);
        })
      }, 10000)
    }


    // 新人专享
    if (wx.getStorageSync('conceal')) {
      this.setData({
        shut: false
      })
    }
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    if (this.data.actiBer[0].status) {
      this.getPreviewNextList()
    }
    if (this.data.actiBer[1].status) {
      this.getUnderwayNextList()
    }

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})